﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UMD.HCIL.Piccolo;
using System.Drawing;
using UMD.HCIL.Piccolo.Nodes;

namespace TreeMap.Graphics
{
	class GraphicsNode : PNode
	{
		public string Name { get; set; }
		public Point TopLeft { get; set; }
		public Point BottomRight { get; set; }
		public Brush Brush { get; set; }
		public bool Horizontal { get; set; }

		public Point NextStartPoint { get; set; }

		private PNode PNode;
		private PText PText;
		private PPath PPath;



		public PCanvas Canvas { get; set; }

		//Initialize Font to use for PText
		private Font MyFont = new Font("Arial", 10);

		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="name"></param>
		/// <param name="canvas"></param>
		public GraphicsNode(string name, PCanvas canvas)
		{
			Canvas = canvas;

			PText = new PText(name);
			PNode = new PNode();
			PPath = new PPath();
			PText.Font = this.MyFont;
			Brush = new System.Drawing.SolidBrush(Color.FromArgb(206, 206, 206));

			//Adding on mouse over events
			PNode.MouseEnter += new PInputEventHandler(PNode_MouseEnter);
			PText.MouseEnter += new PInputEventHandler(PText_MouseEnter);
			PNode.MouseLeave += new PInputEventHandler(PNode_MouseLeave);
			PText.MouseLeave += new PInputEventHandler(PText_MouseLeave);
			PNode.MouseDown += new PInputEventHandler(PNode_MouseDown);
		}

		#region Events

		void PNode_MouseDown(object sender, UMD.HCIL.Piccolo.Event.PInputEventArgs e)
		{
			//Wenn die mittlere Maustaste geklickt wurde
			if (e.Button == System.Windows.Forms.MouseButtons.Middle)
			{
				Graphics.GraphicsManager.GetInstance.ShowClickedBoxOnly(this);
			}
		}
		
		void PText_MouseLeave(object sender, UMD.HCIL.Piccolo.Event.PInputEventArgs e)
		{
			//ToolTip.GetInstance.Visible = false;
			//ToolTip.GetInstance.RemoveToolTip(Canvas);
		}

		void PNode_MouseLeave(object sender, UMD.HCIL.Piccolo.Event.PInputEventArgs e)
		{
			ToolTip.GetInstance.Visible = false;
			ToolTip.GetInstance.RemoveToolTip(Canvas);
		}

		void PText_MouseEnter(object sender, UMD.HCIL.Piccolo.Event.PInputEventArgs e)
		{
			//ToolTip.GetInstance.Visible = true;
			//ToolTip.GetInstance.ShowToolTip(Canvas, PText.Text, TopLeft);
		}

		void PNode_MouseEnter(object sender, UMD.HCIL.Piccolo.Event.PInputEventArgs e)
		{
			ToolTip.GetInstance.Visible = true;
			ToolTip.GetInstance.ShowToolTip(Canvas, PText.Text, TopLeft);
		}
		#endregion

		/// <summary>
		/// Draws the GraphicsNode on the canvas
		/// </summary>
		public void Draw()
		{
			//Offset
			PText.OffsetX = (BottomRight.X - ((BottomRight.X - TopLeft.X) / 2)) - (PText.Width / 2);
			PText.OffsetY = TopLeft.Y;

			PNode.OffsetX = TopLeft.X;
			PNode.OffsetY = TopLeft.Y;

			//Width and Height
			PNode.Width = BottomRight.X - TopLeft.X;
			PNode.Height = BottomRight.Y - TopLeft.Y;

			//Border
			PPath.AddRectangle(TopLeft.X - 1, TopLeft.Y - 1, (BottomRight.X - TopLeft.X) + 2, (BottomRight.Y - TopLeft.Y) + 2);
			PPath.Brush = Brushes.Black;

			//Color of PNode
			PNode.Brush = Brush;

			//Add to canvas
			Canvas.Layer.AddChild(PPath);
			Canvas.Layer.AddChild(PNode);

			//Wenn ein gewisser Treshhold erreicht ist, wird der Text nicht mehr dargestellt
			if (PNode.Width > 50 && PNode.Height > 50)
			{
				Canvas.Layer.AddChild(PText);
			}
		}

		/// <summary>
		/// Calculates Brush of an Node - used for last shown category
		/// </summary>
		public void CalulateBrush()
		{
			Random r = new Random();
			int red = r.Next(0, byte.MaxValue + 1);
			int green = r.Next(0, byte.MaxValue + 1);
			int blue = r.Next(0, byte.MaxValue + 1);
			Brush = new System.Drawing.SolidBrush(Color.FromArgb(red, green, blue));
		}
	}
}
